package org.gwtmultipage.sample.client;

import hr.fer.zemris.rznu.model.dto.user.UserDTO;

import org.gwtmultipage.client.UrlPatternEntryPoint;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.MenuItemSeparator;

import com.google.gwt.core.client.GWT;

@UrlPatternEntryPoint("Results.html(\\\\?.*)?")
public class Results implements EntryPoint {
	
	private RootPanel Wrapper;
	private VerticalPanel PreContainer;
	private LayoutPanel Container;
	private FlowPanel Header;
	private FlowPanel Banner;
	private FlowPanel Content;
	private FlowPanel Footer;
	private VerticalPanel loginContainer;
	private FlowPanel TopMenu;
	private MenuBar menuBar;
	private MenuItem naslovnica;
	private MenuItemSeparator separator;
	private MenuItem ponuda;
	private MenuItemSeparator separator_1;
	private MenuItem rezultati;
	private MenuItemSeparator separator_2;
	private MenuItem statistika;
	private FlowPanel ContentTop;
	private FlowPanel Content1;
	private Label footer;
	private HorizontalPanel login;
	private FlexTable flexTable;
	private Label username;
	private TextBox textBoxUsername;
	private Label password;
	private PasswordTextBox textBoxPassword;
	private Button btnLogIn;
	private Button btnRegister;
	private Image imageHr;
	private Image imageEng;
	private Label bankValue;
	private Button btnLogOut;
	private Button btnProfile;

	
	private BettingConstants constants = GWT.create(BettingConstants.class);
	private BettingMessages messages = GWT.create(BettingMessages.class);
	
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";
	
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	private final InternationalizationServiceAsync interService = GWT.create(InternationalizationService.class);

	public void onModuleLoad() {
		
		Wrapper = RootPanel.get();
		Wrapper.setStyleName("gwt-Template-wrapper");
		Wrapper.setSize("100%", "100%");
		
		PreContainer = new VerticalPanel();
		PreContainer.setBorderWidth(0);
		PreContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Wrapper.add(PreContainer, 0, 0);
		PreContainer.setSize("100%", "100%");
		
		Container = new LayoutPanel();
		PreContainer.add(Container);
		PreContainer.setCellHorizontalAlignment(Container, HasHorizontalAlignment.ALIGN_CENTER);
		Container.setStyleName("gwt-Template-container");
		Container.setSize("960", "800px");
		
		Header = new FlowPanel();
		Header.setStyleName("gwt-Template-header");
		Container.add(Header);
		Header.setWidth("958");
		Container.setWidgetLeftWidth(Header, 0.0, Unit.PX, 964.0, Unit.PX);
		Container.setWidgetTopHeight(Header, 0.0, Unit.PX, 125.0, Unit.PX);
		
		loginContainer = new VerticalPanel();
		loginContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		loginContainer.setStyleName("gwt-Template-topmenu");
		Header.add(loginContainer);
		
		login = new HorizontalPanel();
		login.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		login.setSize("700", "35");
		loginContainer.add(login);
		
		flexTable = new FlexTable();
		login.add(flexTable);
		login.setCellHorizontalAlignment(flexTable, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setSize("466px", "35px");
		
		
		isLogged();
				

		TopMenu = new FlowPanel();
		TopMenu.setStyleName("gwt-Template-login");
		Header.add(TopMenu);
		
		imageHr = new Image("images/hrvatski.gif");
		TopMenu.add(imageHr);
		imageHr.setStyleName("gwt-ImageHr");
		imageHr.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				interService.currentLanguage("hr", new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(Void result) {
						Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						
					}});
			}
		});
		
		imageEng = new Image("images/engleski.gif");
		TopMenu.add(imageEng);
		imageEng.setStyleName("gwt-ImageHr");
		imageEng.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				interService.currentLanguage("eng", new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(Void result) {
						Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						
					}});
			}
		});
		
		menuBar = new MenuBar(false);
		menuBar.setAnimationEnabled(true);
		TopMenu.add(menuBar);
		menuBar.setSize("360", "22");
		
		naslovnica = new MenuItem(constants.home(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						//Window.alert(result);
						if (result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(naslovnica);
		naslovnica.setWidth("90");
		
		separator = new MenuItemSeparator();
		menuBar.addSeparator(separator);
		
		ponuda = new MenuItem(constants.bets(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if(result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(ponuda);
		ponuda.setWidth("90");
		
		separator_1 = new MenuItemSeparator();
		menuBar.addSeparator(separator_1);
		
		rezultati = new MenuItem(constants.results(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if(result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(rezultati);
		rezultati.setWidth("90");
		
		separator_2 = new MenuItemSeparator();
		menuBar.addSeparator(separator_2);
		
		statistika = new MenuItem(constants.statistic(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if(result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/Statistic.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/Statistic.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(statistika);
		statistika.setWidth("90");
		
		Banner = new FlowPanel();
		Banner.setStyleName("gwt-Template-banner");
		Container.add(Banner);
		Banner.setSize("938", "195");
		Container.setWidgetLeftWidth(Banner, 11.0, Unit.PX, 938.0, Unit.PX);
		Container.setWidgetTopHeight(Banner, 106.0, Unit.PX, 250.0, Unit.PX);
		
		Content = new FlowPanel();
		Content.setSize("956", "100%");
		Content.setStyleName("gwt-Template-Content");
		Container.add(Content);
		Container.setWidgetLeftWidth(Content, 0.0, Unit.PX, 960.0, Unit.PX);
		Container.setWidgetTopHeight(Content, 309.0, Unit.PX, 100.0, Unit.PX);
		
		ContentTop = new FlowPanel();
		ContentTop.setStyleName("gwt-Template-ContentTop");
		Content.add(ContentTop);
		
		Content1 = new FlowPanel();
		Content1.setHeight("100%");
		Content1.setStyleName("gwt-Template-Content1");
		Content.add(Content1);
		
		Footer = new FlowPanel();
		Footer.setStyleName("gwt-Template-footer");
		Container.add(Footer);
		Container.setWidgetLeftWidth(Footer, 0.0, Unit.PX, 960.0, Unit.PX);
		Container.setWidgetTopHeight(Footer, 409.0, Unit.PX, 80.0, Unit.PX);
		
		footer = new Label(constants.course() + " | Copyright \u00A9 2010 | VirtualBetting");
		Footer.add(footer);
	}
	
	public void isLogged(){
		loginService.isUserLogged(new AsyncCallback<UserDTO>() {
			
			public void onSuccess(UserDTO result) {
            UserDTO user = (UserDTO) result;
            if (user != null) {

            	flexTable.setSize("330px", "35px");	
        		
        		username = new Label(user.getUsername());
        		username.setStyleName("gwt-User");
        		//user.setSize("100", "22");
        		flexTable.setWidget(0, 1, username);
        		bankValue = new Label("1234.0 HRK");
        		bankValue.setStyleName("gwt-Money");
        		//bankValue.setSize("100", "22");
        		flexTable.setWidget(0, 2, bankValue);
        		
        		btnProfile = new Button(constants.profile());
        		btnProfile.addClickHandler(new ClickHandler() {
        			public void onClick(ClickEvent event) {
        				interService.checkCurrentLanguage(new AsyncCallback<String>() {

        					@Override
        					public void onFailure(Throwable caught) {
        						Window.alert("Error " + caught.getMessage());
        						
        					}

        					@Override
        					public void onSuccess(String result) {
        						if(result.equals("eng") || result.equals("null")) {
        							Window.open("http://127.0.0.1:8888/Profile.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
        						}
        						else if(result.equals("hr")) {
        							Window.open("http://127.0.0.1:8888/Profile.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
        						}
        						
        					}});
        			}
        		});
        		btnProfile.setStyleName("gwt-SignIn");
        		flexTable.setWidget(0, 3, btnProfile);
        		btnProfile.setSize("60", "22");
        		
        		btnLogOut = new Button(constants.logOut());
        		btnLogOut.addClickHandler(new ClickHandler() {
        			public void onClick(ClickEvent event) {
        				
        				loginService.logout(new AsyncCallback<Void>() {
        					
        					public void onSuccess(Void result) {
        						interService.checkCurrentLanguage(new AsyncCallback<String>() {

        							@Override
        							public void onFailure(Throwable caught) {
        								Window.alert("Error " + caught.getMessage());
        								
        							}

        							@Override
        							public void onSuccess(String result) {
        								if(result.equals("eng") || result.equals("null")) {
        									Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
        								}
        								else if(result.equals("hr")) {
        									Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
        								}
        								
        							}});
        		            }
        		        

        		        public void onFailure(Throwable caught) {
        		        	Window.alert("Error " + caught.getMessage());
        		            

        		        }

        			});
        				
        				
        			}
        		});
        		
        		
        		btnLogOut.setStyleName("gwt-SignIn");
        		btnLogOut.setSize("60", "22");				
        		flexTable.setWidget(0, 4, btnLogOut);
            	
            } else {

            	username = new Label(constants.username());
        		flexTable.setWidget(0, 1, username);
        		
        		textBoxUsername = new TextBox();
        		textBoxUsername.setVisibleLength(10);
        		flexTable.setWidget(0, 2, textBoxUsername);
        		textBoxUsername.setSize("100", "18");
        		
        		password = new Label(constants.password());
        		flexTable.setWidget(0, 3, password);
        		
        		textBoxPassword = new PasswordTextBox();
        		textBoxPassword.setVisibleLength(10);
        		flexTable.setWidget(0, 4, textBoxPassword);
        		textBoxPassword.setSize("100", "18");
        		
        		btnLogIn = new Button(constants.logIn());
        		btnLogIn.addClickHandler(new ClickHandler() {
        			public void onClick(ClickEvent event) {
        				login(textBoxUsername.getText(), textBoxPassword.getText());
        				
        			}

        			private void login (String username, String password) {
        				
        				loginService.checkLogin(username, password,
        					new AsyncCallback<UserDTO>() {
        				
        				public void onSuccess(UserDTO result) {
        	            UserDTO user = (UserDTO) result;
        	            if (user != null) {
        	            	interService.checkCurrentLanguage(new AsyncCallback<String>() {

    							@Override
    							public void onFailure(Throwable caught) {
    								Window.alert("Error " + caught.getMessage());
    								
    							}

    							@Override
    							public void onSuccess(String result) {
    								if(result.equals("eng") || result.equals("null")) {
    									Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
    								}
    								else if(result.equals("hr")) {
    									Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
    								}
    								
    							}});
        	            } else {
        	                Window.alert("Invalid Username or Password");
        	                
        	            }
        	        }

        	        public void onFailure(Throwable caught) {
        	        	Window.alert("Error " + caught.getMessage());
        	            

        	        }

        		});}
        		});
        		
        		btnLogIn.setStyleName("gwt-SignIn");
        		btnLogIn.setSize("60", "22");
        		flexTable.setWidget(0, 6, btnLogIn);
        		
        		
        		
        		btnRegister = new Button(constants.register());
        		btnRegister.addClickHandler(new ClickHandler() {
        			public void onClick(ClickEvent event) {
        				interService.checkCurrentLanguage(new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Error " + caught.getMessage());
								
							}

							@Override
							public void onSuccess(String result) {
								if(result.equals("eng") || result.equals("null")) {
									Window.open("http://127.0.0.1:8888/Register.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
								}
								else if(result.equals("hr")) {
									Window.open("http://127.0.0.1:8888/Register.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
								}
								
							}});
        			}
        		});
        		btnRegister.setStyleName("gwt-SignIn");
        		flexTable.setWidget(0, 7, btnRegister);
        		btnRegister.setSize("70", "22");
            	
        }}

        public void onFailure(Throwable caught) {
        	Window.alert("Error " + caught.getMessage());
        }

	});
	}

}